package com.xsf.jieyou.cloud.service;

import com.xsf.jieyou.cloud.api.CloudRedisSmsService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Redis 短信服务接口实现类
 * <p>
 * Description:Dubbo API
 * </p>
 *
 * @author xsf
 * @version v1.0.0
 * @date 2019/11/19 17:01
 * @see com.xsf.jieyou.cloud.service
 **/
@Service(version = "1.0.0")
@RefreshScope
public class CloudRedisSmsServiceImpl implements CloudRedisSmsService {
    /**
     * 用户注册SMS验证码KEY
     */
    @Value("${redis_key_sms_register}")
    private String redisKeySmsRegister;
    /**
     * 用户注册SMS验证码过期时间(单位：分)
     */
    @Value("${redis_key_sms_register_expire_minute}")
    private Integer redisKeySmsRegisterExpireMinute;
    /**
     *  用户登录SMS验证码KEY
     */
    @Value("${redis_key_sms_login}")
    private String redisKeySmsLogin;
    /**
     * 用户登录SMS验证码过期时间(单位：分)
     */
    @Value("${redis_key_sms_login_expire_minute}")
    private Integer redisKeySmsLoginExpireMinute;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private BCryptPasswordEncoder passwordEncoder;

    /**
     * 向 Redis 中存入 用户注册验证码,验证码会经过BCryptPasswordEncoder加密处理
     *
     * @param phoneNumbers 用户手机号
     * @param code         注册验证码
     */
    @Override
    public Integer insertRegisterCodeToRedis(String phoneNumbers, String code) {
        String key = this.redisKeySmsRegister + ":" + phoneNumbers;
        this.redisTemplate.opsForValue().set(key, this.passwordEncoder.encode(code),
                this.redisKeySmsRegisterExpireMinute,
                TimeUnit.MINUTES);
        return 200;
    }

    /**
     * 获取用户注册验证码(已加密)
     *
     * @param phoneNumbers 用户手机号
     * @return code, 注册验证码
     */
    @Override
    public String selectRegisterCodeFromRedis(String phoneNumbers) {
        String key = this.redisKeySmsRegister + ":" + phoneNumbers;
        return (String) this.redisTemplate.opsForValue().get(key);
    }

    /**
     * 向 Redis 中存入 用户登录验证码
     *
     * @param phoneNumbers 用户手机号
     * @param code         登录验证码
     */
    @Override
    public Integer insertLoginCodeToRedis(String phoneNumbers, String code) {
        String key = this.redisKeySmsLogin + ":" + phoneNumbers;
        this.redisTemplate.opsForValue().set(key, this.passwordEncoder.encode(code),
                this.redisKeySmsLoginExpireMinute,
                TimeUnit.MINUTES);
        return 200;
    }

    /**
     * 获取用户登录验证码
     *
     * @param phoneNumbers 用户手机号
     * @return code, 注册验证码
     */
    @Override
    public String selectLoginCodeFromRedis(String phoneNumbers) {
        String key = this.redisKeySmsLogin + ":" + phoneNumbers;
        return (String) this.redisTemplate.opsForValue().get(key);
    }

    /**
     * 删除Redis登录验证码缓存
     *
     * @param phoneNumbers 用户手机号
     */
    @Override
    public Boolean deleteLoginCodeFromRedis(String phoneNumbers) {
        String key = this.redisKeySmsLogin + ":" + phoneNumbers;
        return this.redisTemplate.delete(key);
    }

}
